package org.alino.core.interceptor;

import org.alino.core.Consts;
import org.alino.core.shiro.session.AlinoSessionManager;
import org.alino.core.utils.FileUtils;
import org.alino.core.utils.RequestUtils;
import org.alino.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.AsyncHandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Desc TODO
 * @Author by Brant
 * @Date 2017/1/18
 */
public class BaseInterceptor implements AsyncHandlerInterceptor {
    public static final String BaseInterceptorBeanTag = "baseInterceptor";
    @Autowired
    private CommonsMultipartResolver commonsMultipartResolver;
    @Autowired
    private AlinoSessionManager alinoSessionManager;

    private String urlPath;

    public String getUrlPath() {
        return urlPath;
    }

    public void setUrlPath(String urlPath) {
        this.urlPath = urlPath;
    }

    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        response.setContentType(Consts.APP_CONTENT_TYPE);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {
        if(modelAndView != null){
            ModelMap modelMap = modelAndView.getModelMap();
            String _appPath = request.getContextPath();
            String _appReqUri = request.getRequestURI();
            String _appReqRef = request.getHeader("Referer");
            //请求的request
            modelMap.put("_REQ",request);
            //请求的uri地址
            modelMap.put("_REQ_URL",_appReqUri);
            //上一个页面地址
            modelMap.put("_REQ_REF",_appReqRef);
            //web编码格式
            modelMap.put("_REQ_CHARSET",Consts.APP_CHARSET_UTF8);
            modelMap.put("_REQ_IP", RequestUtils.getIpAddress(request));
            modelMap.put("_REQ_AGENT",RequestUtils.getUserAgent(request));
            modelMap.put("_REQ_IS_IE",RequestUtils.isIEBrowser(request));
            modelMap.put("_REQ_IS_WECHAT",RequestUtils.isWechatBrowser(request));
            modelMap.put("_REQ_IS_MOBILE",RequestUtils.isMoblieBrowser(request));
            //允许上传文件大小
            modelMap.put("_FILE_SIZE", FileUtils.FormatFileSize(commonsMultipartResolver.getFileUpload().getSizeMax()));
            //web根地址
            modelMap.put("_CUSTOM_URL",this.getUrlPath());
            modelMap.put("_ADMIN_URL",_appPath+this.getUrlPath());
            modelMap.put("_BASE_URL",_appPath);
            //TODO 部分URL会出现SessionId为空的情况
            if(StringUtils.isNotBlank(request.getRequestedSessionId())) {
                //modelMap.put("_REQ_TOKEN", alinoSessionManager.getSession(request.getRequestedSessionId()));
            }
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
